package com.example.cdctest.service.impl;

import com.example.cdctest.mapper.ExaminationMapper;
import com.example.cdctest.mapper.ParticipantsMapper;
import com.example.cdctest.mapper.TestPaperMapper;
import com.example.cdctest.mapper.TestPaperStructureMapper;
import com.example.cdctest.pojo.*;
import com.example.cdctest.service.ExaminationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ExaminationServiceImpl implements ExaminationService {

    @Resource
    private ExaminationMapper examinationMapper;

    @Resource
    private TestPaperStructureMapper testPaperStructureMapper;

    @Resource
    private ParticipantsMapper participantsMapper;

    @Resource
    private TestPaperMapper testPaperMapper;

    //添加考试信息，并且返回这个考试信息的Id
    @Override
    public Integer insertExamination(String examinationTitle, Date startTime, Date endTime, Admin admin) {
        Integer examinationId=null;
        Examination examination=new Examination();
        examination.setAdmin(admin);
        examination.setExaminationStartTime(startTime);
        examination.setExaminationEndTime(endTime);
        examination.setExaminationTitle(examinationTitle);
        //考试标题不能重复，先查询
        Examination examination1=examinationMapper.selectExaminationExist(examination);
        if(examination1!=null){  // 如果有，更新
            examinationId=examination1.getId();
            examinationMapper.updateByPrimaryKey(examination);
        }else{  //如果没有，添加
            examinationId=examinationMapper.insert(examination);
        }
        return examinationId;
    }

    //根据条件查出数据,页码,开始日期，截止日期，关键条件
    @Override
    public PageInfo<Examination> findAllExamination(Date logmin, Date logmax, String text, Integer pageNum, Integer pageSize) {
        //一页显示多少条
        PageHelper.startPage(pageNum,pageSize);
        List<Examination> examinations=examinationMapper.selectAllExamination(logmin,logmax,text);
        //默认导航页码是8,可以通过参数设置
        PageInfo<Examination> pageInfo=new PageInfo<>(examinations,10);
        return pageInfo;
    }

    //根据条件查出数量,页码,开始日期，截止日期，关键条件
    @Override
    public Integer getExamination(Date logmin, Date logmax, String text, Integer pageNum, Integer pageSize) {
       //一页显示多少条
        PageHelper.startPage(pageNum,pageSize);
        Integer examinationCount=examinationMapper.selectExaminationCount(logmin,logmax,text);
        return examinationCount;
    }

    //根据考试信息的Id,查出该考试的考题结构
    @Override
    public List<TestPaperStructure> selectTestPaperStructureById(Integer examinationId) {
        return testPaperStructureMapper.selectTestPaperStructureById(examinationId);
    }

    //根据考试信息的Id，查出该考试的所有参加考试人员
    @Override
    public List<TestPaper> selectExaminationParticipantsById(Integer examinationId) {
        return testPaperMapper.selectByPrimaryKey(examinationId);
    }

    //根据考试信息的Id，删除该考试信息
    @Override
    public void updateExaminationById(Integer id) {
        examinationMapper.updateExaminationById(id);
    }

    @Override
    public void insert(Examination object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(Examination object) {

    }

    @Override
    public Examination findById(Integer id) {
        return null;
    }

    @Override
    public List<Examination> findAll() {
        return null;
    }

    @Override
    public PageInfo<Examination> findPage(int pageSize) {
        return null;
    }
}
